<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>考试详情 - AI智能教学培训系统</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" />
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4895ef;
            --light: #f8f9fa;
            --dark: #212529;
        }

        body {
            background-color: #f5f7fb;
            color: var(--dark);
        }

        .app-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, var(--primary), var(--secondary));
            color: white;
            box-shadow: 3px 0 10px rgba(0, 0, 0, 0.1);
            transition: all 0.3s;
            z-index: 100;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
        }

        .logo-container {
            padding: 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .logo {
            font-size: 24px;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon {
            margin-right: 10px;
            font-size: 28px;
        }

        .nav-menu {
            padding: 20px 0;
        }

        .nav-item {
            padding: 12px 25px;
            display: flex;
            align-items: center;
            cursor: pointer;
            transition: all 0.3s;
            border-left: 4px solid transparent;
        }

        .nav-item:hover, .nav-item.active {
            background: rgba(255, 255, 255, 0.1);
            border-left: 4px solid var(--accent);
        }

        .nav-icon {
            margin-right: 12px;
            font-size: 18px;
        }
        .main-content {
            flex: 1;
            margin-left: 260px;
            padding: 30px;
            transition: all 0.3s;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .page-title {
            font-size: 28px;
            font-weight: 600;
            color: var(--dark);
        }

        .exam-info {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            margin-bottom: 30px;
        }

        .question-card {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            margin-bottom: 20px;
        }

        .question-text {
            font-size: 16px;
            margin-bottom: 10px;
        }

        .options label {
            display: block;
            margin-bottom: 8px;
        }

        .submit-btn {
            margin-top: 20px;
        }
        /* 侧边栏链接无下划线且不变色 */
        a.nav-item {
            text-decoration: none;
            color: inherit;
        }
        a.nav-item:visited,
        a.nav-item:active,
        a.nav-item:focus {
            color: inherit;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="app-container">
        <!-- 侧边导航 -->
        <div id="sidebar"></div>

        <!-- 主内容区 -->
        <div class="main-content">
            <!-- 页面标题 -->
            <div class="header">
                <h1 class="page-title">考试详情</h1>
            </div>

            <!-- 考试信息 -->
            <div class="exam-info" v-if="exam && exam.title">
                <h2>{{ exam.title }}</h2>
                <p>教师: {{ exam.instructor }}</p>
                <p>题目数量: {{ exam.questions }}</p>
                <p>考试时长: {{ exam.duration }} 分钟</p>
            </div>

            <!-- 题目列表/已完成试卷（仅用answeredQuestions渲染） -->
            <div v-if="answeredQuestions.length > 0">
                <div class="question-card" v-for="(q, idx) in answeredQuestions" :key="q.usqId">
                    <div class="question-text">{{ idx + 1 }}. {{ q.questionContent }}</div>
                    <div class="options" v-if="q.options">
                        <div v-for="(text, key) in parseOptions(q.options)" :key="key">
                            <span :style="key === q.correctAnswer ? 'color:#4cc9f0;font-weight:bold;' : ''">{{ key }}. {{ text }}</span>
                            <span v-if="key === q.correctAnswer">（正确答案）</span>
                            <span v-if="key === q.userAnswer" style="color:#f72585;">（你的选择）</span>
                        </div>
                    </div>
                    <div style="margin-top:6px;">
                        <span>你的答案：{{ q.userAnswer || '未作答' }}，
                            <span v-if="q.isCorrect">正确</span>
                            <span v-else>错误</span>
                            ，得分：{{ q.scoreEarned || 0 }}
                        </span>
                    </div>
                    <div v-if="q.notes" style="color:#888;">备注：{{ q.notes }}</div>
                </div>
                <div style="margin: 20px 0; font-size: 16px; color: #4361ee;">
                    总分：{{ totalScore }} 分
                </div>
                <div style="color: #4cc9f0;">
                    评语：{{ feedbackText }}
                </div>
            </div>
            <div v-else style="color:#888;text-align:center;margin-top:40px;">
                暂无答题数据，请检查后端接口或数据库内容。
            </div>
            <form v-else @submit.prevent="submitExam">
                <div class="question-card" v-for="(question, index) in exam.questionsList" :key="index">
                    <div class="question-text">{{ index + 1 }}. {{ question.question }}</div>
                    <div class="options">
                        <label v-for="(option, i) in question.options" :key="i">
                            <input type="radio" :name="'q' + index" v-model="answers[index]" :value="option.value" />
                            {{ option.text }}
                        </label>
                    </div>
                </div>
                <el-button type="primary" native-type="submit" class="submit-btn">提交考试</el-button>
            </form>
        </div>
    </div>
</div>

<script>
    new Vue({
        
        el: '#app',
        data: {
            examId: '',
            answeredQuestions: [],
            totalScore: 0,
            feedbackText: '',
            exam: {
                title: '',
                instructor: '',
                questions: 0,
                duration: 0
            }
        },
        mounted() {
            const urlParams = new URLSearchParams(window.location.search);
            this.examId = urlParams.get('examId') || 1;
            // demo模式：examId非数字，显示静态演示数据
            if (!/^[0-9]+$/.test(this.examId)) {
                this.exam = {
                    title: '高等数学期中考试（演示）',
                    instructor: '张老师',
                    questions: 10,
                    duration: 60
                };
                this.answeredQuestions = [
                    {
                        usqId: 1,
                        questionContent: '下列哪个函数是偶函数？',
                        options: '{"A": "f(x) = x", "B": "f(x) = x^2", "C": "f(x) = x^3", "D": "f(x) = sin(x)"}',
                        correctAnswer: 'B',
                        userAnswer: 'B',
                        isCorrect: true,
                        scoreEarned: 2.00,
                        notes: ''
                    },
                    {
                        usqId: 2,
                        questionContent: '极限lim(x→0) sin(x)/x 等于多少？',
                        options: '{"A": "0", "B": "1", "C": "∞", "D": "不存在"}',
                        correctAnswer: 'B',
                        userAnswer: 'B',
                        isCorrect: true,
                        scoreEarned: 2.00,
                        notes: ''
                    },
                    // ...可继续补充更多静态题目...
                ];
                this.totalScore = this.answeredQuestions.reduce((sum, q) => sum + (q.scoreEarned || 0), 0);
                this.feedbackText = this.totalScore === 100 ? '满分，表现优秀！' : (this.totalScore >= 80 ? '答题认真，部分题目需加强理解。' : '需要加强复习。');
                return;
            }
            // 正常模式：examId为数字，请求后端
            fetch(`/exams/${this.examId}/answered-questions`)
                .then(res => res.json())
                .then(resp => {
                    console.log('answeredQuestions接口返回：', resp);
                    if (resp.success && resp.data) {
                        this.answeredQuestions = resp.data;
                        this.totalScore = this.answeredQuestions.reduce((sum, q) => sum + (q.scoreEarned || 0), 0);
                        this.feedbackText = this.totalScore === 100 ? '满分，表现优秀！' : (this.totalScore >= 80 ? '答题认真，部分题目需加强理解。' : '需要加强复习。');
                    }
                    html += `</div>`;
                });
        },
        methods: {
            parseOptions(optStr) {
                if (!optStr) return {};
                try {
                    let fixed = optStr.replace(/\\"/g, '"').replace(/'/g, '"');
                    return JSON.parse(fixed);
                } catch (e) {
                    return {};
                }
            }
        }
    });
</script>
<script>
  fetch('sidebar.html').then(r=>r.text()).then(html=>{document.getElementById('sidebar').innerHTML=html;});
</script>
</body>
</html>
